package org.jetnuke.modules.calendar;


import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.CalendarEvent;
import org.jetnuke.servlets.utilities.StringUtils;
import org.jetnuke.util.JetUtil;

public class DoAddQuickEvent implements IAction {
	String title;
	int startMonth;
	int startDay;
	int startYear;
	int startHour;
	int startMin;
	public void setStartMin(int startMin) {
		this.startMin = startMin;
	}
	public void setStartDay(int startDay) {
		this.startDay = startDay;
	}
	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}
	public void setStartMonth(int startMonth) {
		this.startMonth = startMonth;
	}
	public void setStartYear(int startYear) {
		this.startYear = startYear;
	}
	public void setTitle(String title) {
		this.title = StringUtils.filterText(title,true);
	}
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if(ui==null || ui.getID()<2)
			return null;
		CalendarEvent ce=new CalendarEvent();
		ce.setTITLE(title);
		ce.setFULLDESC(" ");
		ce.setUID(ui.getID());
		ce.setLOCATION(" ");
		ce.setRECURRING(0);
		ce.setTYPE(2);//Appointment
		GregorianCalendar start_date=new GregorianCalendar();
		start_date.set(Calendar.YEAR,startYear);
		start_date.set(Calendar.MONTH,startMonth);
		start_date.set(Calendar.DAY_OF_MONTH,startDay);
		ce.setSTART_DATE(start_date.getTime());
		
		GregorianCalendar start_time=new GregorianCalendar();
		start_time.set(Calendar.HOUR_OF_DAY,startHour);
		start_time.set(Calendar.MINUTE,startMin);
		start_time.set(Calendar.SECOND,0);
		ce.setSTART_TIME(start_time.getTime());
		GregorianCalendar duration=new GregorianCalendar();
		duration.set(Calendar.HOUR_OF_DAY,1);
		duration.set(Calendar.MINUTE,0);
		duration.set(Calendar.SECOND,0);
		ce.setDURATION(duration.getTime());
			

		DataAccessDriver.getInst().newCalendarEventDAO().addNew(ce);
		JetUtil.redirect(vc,response,request.getContextPath()+"/do/calendar");
		return null;
	}
}
